const app = getApp()
//获得全局js变量
var util = require('../../utils/util.js');
Page({
    data: {
        navIndex: 0,
        loadingType: 0,
        loadingText: {
            contentdown: "上拉显示更多",
            contentrefresh: "正在加载...",
            contentnomore: "没有更多数据了"
        },
        p: 1,
        list: [],
        userlon: '',
        userlat: '',
        ifdingwei: false,
        keyword: ''
    },

    onLoad() {
        this.userinfo = wx.getStorageSync('user');
        this.setData({
            userlon: '108.93',
            userlat: '34.27',
            p: 1
        });
    },

    onShow() {
        this.choose_location();
    },

    onInput(e) {
        this.setData({
            keyword: e.detail.value
        });
    },

    search() {
        const key = this.data.keyword;
        if (!key) {
            wx.showToast({
                title: '请输入关键字',
                icon: 'none',
                duration: 1500
            });
            return;
        }
        this.getinfo();
    },

    choose_location() {
        const that = this;
        wx.showLoading({
            title: '定位中...'
        });
        wx.getLocation({
            type: 'gcj02',
            success(res) {
                wx.hideLoading();
                const latitude = res.latitude;
                const longitude = res.longitude;
                wx.setStorageSync('userlon', longitude);
                wx.setStorageSync('userlat', latitude);
                that.setData({
                    userlon: longitude,
                    userlat: latitude,
                    ifdingwei: true
                });
                that.getinfo();
            }
        });
    },

    toDetail(e) {
        const id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '/pages/mallShopDetail/mallShopDetail?id=' + id
        });
    },

    navtab(e) {
        const index = e.currentTarget.dataset.index;
        this.setData({
            navIndex: index,
            p: 1
        });
        this.getinfo();
    },

    getinfo() {
        var that=this;
        util.httpRrquest(app.data.apiurl + 'mallshop/shopList?index=1&keyword='+that.data.keyword+'&type='+that.data.navIndex+'&userlon='+that.data.userlon+'&userlat='+that.data.userlat, 'GET')
        .then(function (response) {
            that.setData({
                list:response.data.data
            });
        }, function (error) {
            console.log(error);
        });
    },

    onReachBottom() {
       
    }
});